package com.lanhe.game.adSetEarnings.service.impl;

import com.lanhe.common.enums.AdSetType;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.game.adSetEarnings.domain.LhAdsetEarnings;
import com.lanhe.game.adSetEarnings.mapper.LhAdsetEarningsMapper;
import com.lanhe.game.adSetEarnings.service.ILhAdsetEarningsService;
import com.lanhe.game.advertising.domain.LhAdsetAdvertisingRec;
import com.lanhe.game.advertising.mapper.LhAdsetAdvertisingRecMapper;
import com.lanhe.game.sysAppUser.domain.SysAppUser;
import com.lanhe.game.sysAppUser.mapper.SysAppUserMapper;
import com.lanhe.member.mapper.LhMemberConsumedRecMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 看广软件的收益分发Service业务层处理
 * 
 * @author tianliang
 * @date 2024-02-06
 */
@Service
public class LhAdsetEarningsServiceImpl implements ILhAdsetEarningsService 
{
    @Autowired
    private LhAdsetEarningsMapper lhAdsetEarningsMapper;

    @Autowired
    private LhAdsetAdvertisingRecMapper lhAdsetAdvertisingRecMapper;

    @Autowired
    private SysAppUserMapper sysAppUserMapper;

    /**
     * 查询看广软件的收益分发
     * 
     * @param adId 看广软件的收益分发主键
     * @return 看广软件的收益分发
     */
    @Override
    public LhAdsetEarnings selectLhAdsetEarningsByAdId(Long adId)
    {
        return lhAdsetEarningsMapper.selectLhAdsetEarningsByAdId(adId);
    }

    /**
     * 查询看广软件的收益分发列表
     * 
     * @param lhAdsetEarnings 看广软件的收益分发
     * @return 看广软件的收益分发
     */
    @Override
    public List<LhAdsetEarnings> selectLhAdsetEarningsList(LhAdsetEarnings lhAdsetEarnings)
    {
        return lhAdsetEarningsMapper.selectLhAdsetEarningsList(lhAdsetEarnings);
    }

    /**
     * 新增看广软件的收益分发
     * 
     * @param lhAdsetEarnings 看广软件的收益分发
     * @return 结果
     */
    @Override
    public int insertLhAdsetEarnings(LhAdsetEarnings lhAdsetEarnings)
    {
        lhAdsetEarnings.setCreateTime(DateUtils.getNowDate());
        return lhAdsetEarningsMapper.insertLhAdsetEarnings(lhAdsetEarnings);
    }

    /**
     * 修改看广软件的收益分发
     * 
     * @param lhAdsetEarnings 看广软件的收益分发
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLhAdsetEarnings(LhAdsetEarnings lhAdsetEarnings)
    {

        BigDecimal mVideoDivide = new BigDecimal(0);   //激励视频的平均值
        BigDecimal TpAdvertisingDivide = new BigDecimal(0);   //插屏广告的平均值
        BigDecimal bannerDivide = new BigDecimal(0);  // banner的平均值

        System.err.println("lhAdsetEarnings  "+lhAdsetEarnings);
        // 查询 收益者
        Date createTime = lhAdsetEarnings.getCreateTime();
        List<LhAdsetAdvertisingRec> lhAdsetAdvertisingRecs = lhAdsetAdvertisingRecMapper.selectLhAdsetAdvertisingRecByTime(createTime);

        // 获取到了 激励视频的收益
        BigDecimal videoEarnings = lhAdsetEarnings.getmVideoEarnings();
        if (lhAdsetEarnings.getmVideoCount() != 0L){
            BigDecimal count = new BigDecimal(lhAdsetEarnings.getmVideoCount());
            System.err.println("count "+count);
            mVideoDivide = videoEarnings.divide(count, 2, BigDecimal.ROUND_DOWN);

        }
        // 获取到了 插屏的收益
        BigDecimal tpAdvertisingEarnings = lhAdsetEarnings.getTpAdvertisingEarnings();
        if (lhAdsetEarnings.getTpAdvertisingCount() != 0L){
            BigDecimal count = new BigDecimal(lhAdsetEarnings.getTpAdvertisingCount());
            TpAdvertisingDivide = tpAdvertisingEarnings.divide(count, 2, BigDecimal.ROUND_DOWN);
        }
        //获取到了 banner的收益
        BigDecimal bannerAdvertisingEarnings = lhAdsetEarnings.getBannerAdvertisingEarnings();
        if (lhAdsetEarnings.getBannerAdvertisingCount() != 0L){
            BigDecimal count = new BigDecimal(lhAdsetEarnings.getBannerAdvertisingCount());
            bannerDivide = bannerAdvertisingEarnings.divide(count, 2, BigDecimal.ROUND_DOWN);
        }
        for (LhAdsetAdvertisingRec lhAdsetAdvertisingRec : lhAdsetAdvertisingRecs) {
            SysAppUser sysAppUser = sysAppUserMapper.selectSysAppUserByOpenId(lhAdsetAdvertisingRec.getOpenId());
            BigDecimal count = new BigDecimal(lhAdsetAdvertisingRec.getCount());     // 收益人的点击次数
            BigDecimal totalAmount = sysAppUser.getTotalAmount();     // 收益人的钱包
            // 获取到了收益人
            if (AdSetType.M_video.getCode().equals(lhAdsetAdvertisingRec.getAdType())){
                System.err.println("激励视频平均值收益 "+ mVideoDivide);
                // 这是激励视频的收益
                BigDecimal multiply = mVideoDivide.multiply(count);
                System.err.println("激励视频获得的收益  "+ multiply);
                BigDecimal money = totalAmount.add(multiply);
                sysAppUser.setTotalAmount(money);

            }
            if (AdSetType.Tp_advertising.getCode().equals(lhAdsetAdvertisingRec.getAdType())){
                //这是插屏广告的人
                System.err.println("插屏广告平均值收益 "+ TpAdvertisingDivide);
                BigDecimal multiply = TpAdvertisingDivide.multiply(count);
                System.err.println("插屏广告获得的收益  "+ multiply);
                BigDecimal money = totalAmount.add(multiply);
                sysAppUser.setTotalAmount(money);
            }
            if (AdSetType.Banner_advertising.getCode().equals(lhAdsetAdvertisingRec.getAdType())){
                //这是banner广告收益的人
                System.err.println("banner平均值收益 "+ bannerDivide);
                BigDecimal multiply = bannerDivide.multiply(count);
                System.err.println("banner获得的收益  "+ multiply);
                BigDecimal money = totalAmount.add(multiply);
                sysAppUser.setTotalAmount(money);
            }
            sysAppUser.setUpdateTime(new Date());
            sysAppUserMapper.updateSysAppUser(sysAppUser);
        }
        lhAdsetEarnings.setUpdateTime(DateUtils.getNowDate());
        lhAdsetEarnings.setState(1);
        return lhAdsetEarningsMapper.updateLhAdsetEarnings(lhAdsetEarnings);
    }

    /**
     * 批量删除看广软件的收益分发
     * 
     * @param adIds 需要删除的看广软件的收益分发主键
     * @return 结果
     */
    @Override
    public int deleteLhAdsetEarningsByAdIds(Long[] adIds)
    {
        return lhAdsetEarningsMapper.deleteLhAdsetEarningsByAdIds(adIds);
    }

    /**
     * 删除看广软件的收益分发信息
     * 
     * @param adId 看广软件的收益分发主键
     * @return 结果
     */
    @Override
    public int deleteLhAdsetEarningsByAdId(Long adId)
    {
        return lhAdsetEarningsMapper.deleteLhAdsetEarningsByAdId(adId);
    }
}
